package cn.jr.dao.impl;

import cn.jr.dao.IOrderDao;
import cn.jr.dao.IOrderProjectDao;
import cn.jr.pojo.Order;
import cn.jr.pojo.OrderProject;
import cn.jr.util.DBHelper;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class OrderDaoImpl implements IOrderDao {
    DBHelper db=new DBHelper();
    IOrderProjectDao iopd=new OrderProjectDaoImpl();
    @Override
    public int insertOrder(double priceAll,double priceAllSJ) {
        /**
         * 新增订单
         * @param priceAll 订单金额
         * @param priceAllSJ 订单金额（包含活动金额）
         * @return 影响行数
         */
        String sql="insert into torder values(?,?,?,?,?)";
        String orderIdEnd= iopd.selectOrderProjectEnd().getOrderId();
        int orderNumberInt =Integer.parseInt(selectOrderEnd().substring(2))+1;
        String orderNumberStr="SR"+orderNumberInt;
        Date now=new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String orderTime=sdf.format(now);
        Object[] objects={orderIdEnd,orderNumberStr,priceAll,priceAllSJ,orderTime};
        return db.dtUpdate(sql,objects);
    }

    @Override
    public String selectOrder(String orderNumber) {
        /**
         * 根据订单编号查询订单主键
         * @param orderNumber 订单编号
         * @return 订单主键
         */
        String sql="select * from torder where orderNumber=?";
        ArrayList<Order> list=db.dtQuery(sql,Order.class,orderNumber);
        return list.size()>0?list.get(0).getOrderId():null;
    }

    @Override
    public String selectOrderByOrderId(String orderId) {
        /**
         * 根据订单主键查询订单编号
         * @param orderId 订单主键
         * @return 订单编号
         */
        String sql="select * from torder where orderId=?";
        ArrayList<Order> list=db.dtQuery(sql,Order.class,orderId);
        return list.size()>0?list.get(0).getOrderNumber():null;
    }

    @Override
    public String selectOrderEnd() {
        /**
         * 查询订单编号最大值
         * @return 订单编号最大值
         */
        String sql="select * from torder";
        ArrayList<Order> list=db.dtQuery(sql,Order.class);
        return list.get(list.size()-1).getOrderNumber();
    }

    @Override
    public ArrayList<Order> selectOrderByTime(String startTime, String endTime) {
        /**
         * 根据时间查询订单
         * @param startTime 开始时间
         * @param endTime 结束时间
         * @return 订单集合
         */
        String sql="select * from torder where orderTime between ? and ?";
        return db.dtQuery(sql,Order.class,startTime,endTime);
    }
}
